#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e4 + 5;

int n, m, p, q;
int f[N], sz[N];
void init(int n) { rep(i, 1, n) f[i] = i, sz[i] = 1; }
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
void merge(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx == fy) return;
  sz[fx] += sz[fy];
  f[fy] = fx;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m >> p >> q;
  init(n);
  while (p--) {
    int x, y;
    cin >> x >> y;
    merge(x, y);
  }
  int cnt1 = sz[find(1)];
  init(m);
  while (q--) {
    int x, y;
    cin >> x >> y;
    x = -x, y = -y;
    merge(x, y);
  }
  int cnt2 = sz[find(1)];
  cout << min(cnt1, cnt2);
  return 0;
}